 package com.demo.service;

import com.demo.model.Order;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class OrderService {
    private final Map<Long, Order> orderRepo = new HashMap<>();
    private long idCounter = 1;

    public List<Order> getAllOrders() {
        return new ArrayList<>(orderRepo.values());
    }
    public Order getOrderById(Long id) {
        return orderRepo.get(id);
    }
    public void addOrder(Order order) {
        order.setId(idCounter++);
        orderRepo.put(order.getId(), order);
    }
    public void updateOrder(Order order) {
        orderRepo.put(order.getId(), order);
    }
    public void deleteOrder(Long id) {
        orderRepo.remove(id);
    }
    // 批量添加
    public void addOrders(List<Order> orders) {
        for (Order order : orders) {
            addOrder(order);
        }
    }
    // 分页查询
    public List<Order> getOrdersByPage(int pageNum, int pageSize) {
        List<Order> all = getAllOrders();
        int fromIndex = Math.max(0, (pageNum - 1) * pageSize);
        int toIndex = Math.min(all.size(), fromIndex + pageSize);
        if (fromIndex > toIndex) return Collections.emptyList();
        return all.subList(fromIndex, toIndex);
    }
} 